﻿using AudioMonitor.Properties;
using AudioSourceManager;
using NAudio.CoreAudioApi;
using System;
using System.Windows.Forms;

namespace AudioMonitor
{
    public partial class AudioMiniWindow : UserControl
    {
        public Action<string, string> OnError;

        public Action<AudioMiniWindow> OnClick;

        public bool Mute = true;

        public AudioPlayInfo PlayInfo { get; private set; }

        private AudioVolumeMonitor _volumeMonitor;

        public AudioMiniWindow()
        {
            InitializeComponent();
            tableLayoutPanel1.Click += Form_Tick;
            this.Click += Form_Tick;
            tableLayoutPanel2.Click += Form_Tick;
            volumeBarControl1.Click += Form_Tick;
            volumeBarControl2.Click += Form_Tick;
            lblFormat.Click += Form_Tick;
        }

        public void StartMonitoring(AudioConverterInfo converterInfo)
        {
            PlayInfo = converterInfo.PlayInfo;
            _volumeMonitor = new AudioVolumeMonitor(PlayInfo, 25);
            _volumeMonitor.Start();
            lblFormat.Text = $"{converterInfo.Format}[{converterInfo.PID}]";
            lblName.Text = converterInfo.AudioName;
            lblPID.Text = $"UDP";
        }

        private void Form_Tick(object sender, EventArgs e)
        {
            OnClick?.Invoke(this);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.BackgroundImage = Mute ? null : Resources.红色边框;
        }

        internal void UpdateVolume()
        {
            volumeBarControl1.Volume = _volumeMonitor.VolumeLeft;
            volumeBarControl2.Volume = _volumeMonitor.VolumeRight;
        }
    }
}
